package org.jing.ext.media.m3u8.tag;

import org.jing.core.util.ClassUtil;
import org.jing.core.util.GenericUtil;
import org.jing.core.util.StringUtil;
import org.jing.ext.media.m3u8.BaseM3U8Tag;
import org.jing.ext.media.m3u8.M3U8TagType;
import org.jing.ext.media.m3u8.M3U8TagTypeInfo;

import java.util.List;

/**
 * Description: <br>
 *
 * @author bksmate <br>
 * @since 2024-12-24 <br>
 */
@M3U8TagTypeInfo(M3U8TagType.EXT_INFO)
public class M3U8MediaPartition extends BaseM3U8Tag {
    private double duration;

    private String name;

    private String media;

    @Override public void setParameter(String value) {
        String[] p = value.split(",");
        setDuration(ClassUtil.cast2Number(StringUtil.getStringFromArray(p, 0, "0").trim()).doubleValue());
        setName(StringUtil.getStringFromArray(p, 1, "").trim());
    }

    @Override public void setExtParameters(List<String> parameters) {
        if (GenericUtil.count(parameters) > 0) {
            setMedia(StringUtil.ifEmpty(parameters.get(0)).trim());
        }
    }

    @Override protected String outputValue() {
        return String.format("%.6f", duration) + "," + StringUtil.ifEmpty(name) + "\n" + media;
    }

    public double getDuration() {
        return duration;
    }

    public void setDuration(double duration) {
        this.duration = duration;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMedia() {
        return media;
    }

    public void setMedia(String media) {
        this.media = media;
    }
}
